package com.gouying.mall.rabbit.mq;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * toptic 在绑定的时候添加路由键 *，# 可以模糊匹配
 *  绑定时候topic.route.*
 *  生产topic.route.one能发送成功topic.route.one.two这样就不行了
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TopicRabbitMq {

    private static final String  exchange = "exchange.topic";

    private static final String  queue = "queue.topic";

    private static final String routingKey = "topic.route.*";



    @Autowired
    RabbitAdmin rabbitAdmin;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void createChange() {
        rabbitAdmin.declareExchange(new TopicExchange(exchange, true, false, null));
    }

    @Test
    public void createQueue() {
        rabbitAdmin.declareQueue(new Queue(queue, true, false, false, null));
    }

    @Test
    public void binding() {
        // 目的地就是交换机发送到哪的地方 这里发送到队列
        String destination = queue; // 【自的地】
        Binding.DestinationType destinationType = Binding.DestinationType.QUEUE; //【目的地类型】
        Map<String, Object> arguments = new HashMap<>();
        rabbitAdmin.declareBinding(new Binding(destination,destinationType,exchange,routingKey,arguments));
    }


    @Test
    public void send(){
        rabbitTemplate.convertAndSend(exchange,"topic.route.*","test-routing-value-two");
    }

//    @Test
//    public void getMsg(){
//        Message receive = rabbitTemplate.receive("test-queue");
//        System.out.println(JSON.toJSONString(receive));
//    }

}
